package de.bazinga.rotw.game;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jmex.audio.AudioTrack;
import com.jmex.game.StandardGame;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.menu.CreditsMenu;
import de.bazinga.rotw.menu.GameMenu;
import de.bazinga.rotw.menu.InfoMenu;
import de.bazinga.rotw.menu.IngameMenu;
import de.bazinga.rotw.menu.InternetMenu;
import de.bazinga.rotw.menu.LANMenu;
import de.bazinga.rotw.menu.LevelMenu;
import de.bazinga.rotw.menu.LoadingState;
import de.bazinga.rotw.menu.MainMenu;
import de.bazinga.rotw.menu.OptionsMenu;
import de.bazinga.rotw.menu.ServerMenu;
import de.bazinga.rotw.menu.SplashScreen;
import de.bazinga.rotw.sound.Sound;
import de.bazinga.rotw.statistics.StatisticState;

public class Game {
	private static StandardGame game = null;
	
	private static AudioTrack backgroundMusic = null;
	
	public Game() {
		TextureManager.COMPRESS_BY_DEFAULT = false;
		
		game = new StandardGame("Return of the Warlord");
		
		
		// Get Settings
		try{
			if(!Settings.prompt(game.getSettings(), "Return of the Warlord")) {
				// User pressed Cancel-Button
				return ;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		// Start game
		DisplaySystem.getDisplaySystem().setMinSamples(4);
		game.start();
		
		// Turn of Logger!
		Logger.getLogger("com.jme").setLevel(Level.SEVERE);
		
		// Show Loading-Screen first
		Future<Object> waitTillDone = GameTaskQueueManager.getManager().update(new Callable<Object>() {
			public Object call() throws Exception {				
				// InfoWindow
				/*InfoWindow infoState = new InfoWindow();
				GameStateManager.getInstance().attachChild(infoState);
				infoState.setActive(false);
				*///States.getInstance().addState("infoState", infoState);
				

				LoadingState loadingState = new LoadingState();
				GameStateManager.getInstance().attachChild(loadingState);
				loadingState.setActive(true);
				//States.getInstance().addState("loadingState", loadingState);
				
				return null;
			}
		});
		// Wait until Done
		try {
			waitTillDone.get();
		} catch (InterruptedException e) {
		} catch (Exception e) {
		}	
				
		// Now load other stuff
		waitTillDone = GameTaskQueueManager.getManager().update(new Callable<Object>() {
			public Object call() throws Exception {
				RotwGameState gameState = new RotwGameState();
				GameStateManager.getInstance().attachChild(gameState);
				gameState.setActive(false);
				//States.getInstance().addState("gameState", gameState);
				
				
				
				StatisticState statState = new StatisticState();
				GameStateManager.getInstance().attachChild(statState);
				statState.setActive(false);
				//States.getInstance().addState("statisticState", statState);
				
				
				/**
				 * MENU
				 */
				MainMenu menuState = new MainMenu();
				GameStateManager.getInstance().attachChild(menuState);
				menuState.setActive(false);
				//States.getInstance().addState("menuState", menuState);
				

				GameMenu gameMenuState = new GameMenu();
				GameStateManager.getInstance().attachChild(gameMenuState);
				gameMenuState.setActive(false);
				//States.getInstance().addState("gameMenuState", gameMenuState);
				
				// New Server
				ServerMenu serverMenuState = new ServerMenu();
				GameStateManager.getInstance().attachChild(serverMenuState);
				serverMenuState.setActive(false);
				//States.getInstance().addState("serverMenuState", serverMenuState);
				
				
				// LAN
				LANMenu lanMenuState = new LANMenu();
				GameStateManager.getInstance().attachChild(lanMenuState);
				lanMenuState.setActive(false);
				//States.getInstance().addState("lanMenuState", lanMenuState);
				
				
				// Internet
				InternetMenu internetMenuState = new InternetMenu();
				GameStateManager.getInstance().attachChild(internetMenuState);
				internetMenuState.setActive(false);
				//States.getInstance().addState("internetMenuState", internetMenuState);
				
				
				// IngameMenu
				IngameMenu ingameMenuState = new IngameMenu();
				GameStateManager.getInstance().attachChild(ingameMenuState);
				ingameMenuState.setActive(false);
				//States.getInstance().addState("ingameMenuState", ingameMenuState);
				
				
				
				
				
				
				LevelMenu levelMenuState = new LevelMenu();
				GameStateManager.getInstance().attachChild(levelMenuState);
				levelMenuState.setActive(false);
				//States.getInstance().addState("levelMenuState", levelMenuState);
				
				OptionsMenu optionsMenuState = new OptionsMenu();
				GameStateManager.getInstance().attachChild(optionsMenuState);
				optionsMenuState.setActive(false);
				//States.getInstance().addState("optionsMenuState", optionsMenuState);
				
				CreditsMenu creditsMenuState = new CreditsMenu();
				GameStateManager.getInstance().attachChild(creditsMenuState);
				creditsMenuState.setActive(false);
				//States.getInstance().addState("creditsMenuState", creditsMenuState);
				
				InfoMenu infoMenuState = new InfoMenu();
				GameStateManager.getInstance().attachChild(infoMenuState);
				infoMenuState.setActive(false);
				//States.getInstance().addState("infoMenuState", infoMenuState);
				

				GameStateManager.getInstance().getChild("loadingMenu").setActive(false);
				//States.getInstance().getState("loadingState").setActive(false);
				SplashScreen splashScreen = new SplashScreen();
				GameStateManager.getInstance().attachChild(splashScreen);
				splashScreen.setActive(true);
				
				return null;
			}
        });
		
		// Wait until Done
		try {
			waitTillDone.get();
		} catch (InterruptedException e) {
		} catch (Exception e) {
		}
		
		Sound.getInstance().addSound("sounds/menu.wav");
		backgroundMusic = Sound.getInstance().getAudioTrack("sounds/menu.wav");
		backgroundMusic.setLooping(true);
		startMenuSound();
	}
	
	public static StandardGame getGame() {
		return game;
	}
	
	public static void main(String[] args) {
		new Game();
	}
	
	
	public static void startMenuSound() {
		if(Game.getGame().getSettings().isSFX()) backgroundMusic.play();
	}
	public static void stopMenuSound() {
		if(Game.getGame().getSettings().isSFX()) backgroundMusic.stop();
	}
}